package com.cooksys.film_rental.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooksys.film_rental.bo.CountryBo;
import com.cooksys.film_rental.dao.CountryDao;
import com.cooksys.film_rental.model.City;
import com.cooksys.film_rental.model.Country;

@Service("CountryBo")
public class CountryBoImpl implements CountryBo {

	@Autowired(required = true)
	CountryDao countryDao;

	@Override
	public Country save(Country country) {
		return this.countryDao.save(country);
	}

	@Override
	public void update(Country country) {
		this.countryDao.update(country);
	}

	@Override
	public void delete(Country country) {
		this.countryDao.delete(country);
	}

	@Override
	public Country getCountryByCountryId(Short countryId) {
		return this.countryDao.getCountryByCountryId(countryId);
	}

	@Override
	public Country getCountryByCountryId(String countryId) {
		try {

			Short countryIdAsShort = Short.valueOf(countryId);
			return this.getCountryByCountryId(countryIdAsShort);

		} catch (NumberFormatException e) {

			System.err
					.println("Could not parse Short value for countryId from string: "
							+ countryId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;

		}
	}

	@Override
	public Country getCountryByCity(City city) {
		return this.countryDao.getCountryByCity(city);
	}

	@Override
	public List<Country> searchCountriesByPartialCountryName(
			String partialCountryName) {
		return this.countryDao
				.searchCountriesByPartialCountryName(partialCountryName);
	}

	@Override
	public List<Country> getAllCountries() {
		return this.countryDao.getAllCountries();
	}

	@Override
	public Country matchCountryToDatabase(Country country) {
		
		Country result = null;
		
		if(country.getCountryId() != null){
			
			result = this.getCountryByCountryId(country.getCountryId());
			
		}
		
		if(result == null){
			
			result = this.countryDao.getCountryByInfo(country.getCountry());
			
		}
		
		return result;

	}

}
